﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace Clipboard.Tools
{
    public class TextFormatterConverter : IValueConverter
    {
     
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;


            int maxLength = 50;  // 默认最大长度
            if (parameter != null && int.TryParse(parameter.ToString(), out int paramLength))
            {
                maxLength = paramLength;
            }

            string str = value.ToString();
            // 去掉开头的空格和换行符，替换中间的换行符为空格
            string formattedText = string.IsNullOrWhiteSpace(str) ? string.Empty :
                string.Join(" ", str.TrimStart().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));


            //// 如果文本长度超过最大长度，截取并添加省略号
            //if (formattedText.Length > maxLength)
            //{
            //    formattedText = formattedText.Substring(0, maxLength) + "...";
            //}

            return formattedText;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;  // 不需要实现反向转换
        }
    }
}
